package com.ktg.mes.wm.service.impl;

import java.util.Arrays;
import java.util.List;
import com.ktg.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.ktg.mes.wm.mapper.WmItemSplitMapper;
import com.ktg.mes.wm.domain.WmItemSplit;
import com.ktg.mes.wm.service.IWmItemSplitService;



/**
 * 物料拆分Service业务层处理
 *
 * @author byteco
 * @date 2023-01-06
 */
@Service
public class WmItemSplitServiceImpl extends ServiceImpl<WmItemSplitMapper, WmItemSplit> implements IWmItemSplitService
{




    /**
     * 查询物料拆分
     *
     * @param entity 物料拆分
     * @param page 分页对象
     * @return 物料拆分
     */
    @Override
    public IPage<WmItemSplit> queryPageList(WmItemSplit entity, Page<WmItemSplit> page) {
        QueryWrapper<WmItemSplit> lqw = new QueryWrapper<WmItemSplit>(entity);
        lqw.orderByDesc("create_time");
        IPage<WmItemSplit> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询物料拆分
     *
     * @param entity 物料拆分
     * @return 物料拆分
     */
    @Override
    public List<WmItemSplit> queryList(WmItemSplit entity) {
        QueryWrapper<WmItemSplit> lqw = new QueryWrapper<WmItemSplit>(entity);
        lqw.orderByDesc("create_time");
        return this.list(lqw);
    }

    public WmItemSplit getInfo(Long id)
    {
        return this.getById(id);
    }


    /**
     * 新增物料拆分
     *
     * @param entity 物料拆分
     * @return 结果
     */
    @Override
    public void insert(WmItemSplit entity)
    {
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        this.save(entity);
    }

    /**
     * 修改物料拆分
     *
     * @param entity 物料拆分
     * @return 结果
     */
    @Override
    public boolean update(WmItemSplit entity)
    {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(entity);
    }

    /**
     * 批量删除物料拆分
     *
     * @param lineIds 需要删除的物料拆分主键
     * @return 结果
     */
    @Override
    public boolean delete(Long[] lineIds)
    {

        return this.removeByIds(Arrays.asList(lineIds));
    }

    /**
     * 删除物料拆分信息
     *
     * @param lineId 物料拆分主键
     * @return 结果
     */
    @Override
    public boolean delete(Long lineId)
    {
        return this.removeById(lineId);
    }

    private LambdaQueryWrapper<WmItemSplit> buildQueryWrapper(WmItemSplit entity) {
        LambdaQueryWrapper<WmItemSplit> lqw = Wrappers.lambdaQuery();
                    lqw.eq(entity.getItemId() != null, WmItemSplit::getItemId, entity.getItemId());
                    lqw.eq(entity.getWarehouseId() != null, WmItemSplit::getWarehouseId, entity.getWarehouseId());
                    lqw.eq(entity.getQuantity() != null, WmItemSplit::getQuantity, entity.getQuantity());
                    lqw.eq(StringUtils.isNotBlank(entity.getBatchCode()), WmItemSplit::getBatchCode, entity.getBatchCode());
                    lqw.eq(StringUtils.isNotBlank(entity.getMeterialCode()), WmItemSplit::getMeterialCode, entity.getMeterialCode());
                    lqw.eq(StringUtils.isNotBlank(entity.getIsInStock()), WmItemSplit::getIsInStock, entity.getIsInStock());
                    lqw.eq(entity.getFromItemRecptLineId() != null, WmItemSplit::getFromItemRecptLineId, entity.getFromItemRecptLineId());
        return lqw;
    }
}
